package com.shyroke.daydayzhuansvc.util;

import cn.hutool.core.collection.CollectionUtil;
import com.shyroke.daydayzhuanapi.dto.AdvertDto;
import com.shyroke.daydayzhuanapi.dto.CommentDto;
import com.shyroke.daydayzhuanapi.dto.LabelDto;
import com.shyroke.daydayzhuanapi.dto.TypeDto;
import com.shyroke.daydayzhuanapi.entity.FriendsEntity;
import com.shyroke.daydayzhuanapi.entity.LabelEntity;
import com.shyroke.daydayzhuanapi.entity.RoleEntity;
import com.shyroke.daydayzhuanapi.entity.TypeEntity;
import com.shyroke.daydayzhuansvc.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 类名 ：用户的工具类
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2018/12/18 14:39
 */
@Component
public class UserUtils {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

//    @Autowired
//    private TypeMapper typeMapper;

    @Autowired
    private ITypeService typeService;


//    @Autowired
//    private AdvertMapper advertMapper;
    @Autowired
    private IAdvertService advertService;

    @Autowired
    private IOSSService ossService;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private ILableService lableService;

    public static UserUtils userUtils;

    @Autowired
    private IFriendService friendService;

    /**
     * 是否管理员
     * @param roleList
     * @return
     */
    public static Boolean isAdmin2(Set<RoleEntity> roleList) {
        boolean flag = false;
        for(RoleEntity role:roleList){
            if("admin".equals(role.getName())){
                flag = true;
            }
        }
        return flag;
    }

    @PostConstruct
    public void init(){
        userUtils = this;
        userUtils.typeService = this.typeService;
        userUtils.advertService = this.advertService;
        userUtils.ossService = this.ossService;
        userUtils.friendService = this.friendService;
    }

    /**
     * 校验当前登录用户是否有该权限
     * @param permissionname 权限名称
     * @return
     */
    public static boolean isPermission(String permissionname) {

        Subject subject = SecurityUtils.getSubject();

        if(subject.isPermitted(permissionname)){
            return true;
        }else{
            return false;
        }

    }


    /**
     * 获取菜单列表和广告
     * @return
     */
    public static  ModelAndView getMenuList(ModelAndView model,Integer typeId){
        List<TypeDto> typeList =  userUtils.typeService.getTypeList();

        List<TypeDto> parentList = new ArrayList<>();
        List<TypeDto> childList = new ArrayList<>();
        List<TypeDto> result = new ArrayList<>();

        for(TypeDto type:typeList){
            if(0 == type.getParentId()){
                parentList.add(type);
            }else{
                childList.add(type);
            }
        }

        for(TypeDto parent:parentList){
            List<TypeDto> childMenus = parent.getTypeList();
            for(TypeDto menu:childList){
                if(menu.getParentId() == parent.getId()){
                    childMenus.add(menu);
                    parent.setTypeList(childMenus);
                }
            }
            result.add(parent);
        }
        //将网赚闲谈放到最后
        Iterator<TypeDto> iterator = result.iterator();
        TypeDto wzType = null;
        while (iterator.hasNext()){
            TypeDto next = iterator.next();
            if(10 == next.getId()){
                wzType = next;
                iterator.remove();
                break;
            }
        }
        result.add(wzType);

        //友情链接
        List<FriendsEntity> friendSiteList = userUtils.friendService.getList();

        /**
         * 获取首页广告图片
         */
        List<AdvertDto> adverts = userUtils.advertService.getList();
        if(CollectionUtil.isNotEmpty(adverts)){
            adverts = adverts.stream().map(advertDto -> {
                advertDto.setUrl(userUtils.ossService.getImgUrl(advertDto.getUrl()));
                return advertDto;
            }).collect(Collectors.toList());
        }

        /**
         * 获取最新10条评论
         */
        List<CommentDto> commentList = userUtils.commentService.getNewComment();

        /**
         * 获取赚钱热词
         */
       List<LabelDto> hotKeyList = userUtils.lableService.getHotkeyList();

        model.addObject("menuList",result);
        model.addObject("friends",friendSiteList);
        model.addObject("advertList",adverts);
        model.addObject("commentList",commentList);
        model.addObject("hotKeyList",hotKeyList);
//        model.addObject("labelList",typeList);

        return model;
    }


}
